package cn.com.open.itembank.common;

import cn.com.open.itembank.dto.*;
import cn.com.open.itembank.entity.*;
import cn.com.open.itembank.utils.DateUtil;
import cn.com.open.itembank.utils.StringUtil;

import java.util.*;

/**
 * Created by lucky on 2017/9/13.
 */
public class ModelTransfer {

    private ModelTransfer() {
    }

    //region 从试题删除模型得到试题模型

    /**
     * 从试题删除模型得到试题模型
     *
     * @param del the del
     * @return the question
     */
    public static Question copyFromDeleted(QuestionDeleted del) {
        Question model = new Question();

        model.setQuestionId(del.getQuestionId());
        model.setQuestionTypeId(del.getQuestionTypeId());
        model.setQuestionCategory(del.getQuestionCategory());
        model.setDifficulty(del.getDifficulty());
        model.setTitle(del.getTitle());
        model.setComment(del.getComment());
        model.setScore(del.getScore());
        model.setKeywords(del.getKeywords());
        model.setQuestionAnalysis(del.getQuestionAnalysis());
        model.setFieldCount(del.getFieldCount());
        model.setCreater(del.getCreater());
        model.setCreateTime(del.getCreateTime());
        model.setModifier(del.getModifier());
        model.setModifyTime(del.getModifyTime());
        model.setStatus(del.getStatus());
        model.setIsCheck(del.getIsCheck());
        model.setChecker(del.getChecker());
        model.setCheckTime(del.getCheckTime());
        model.setLastUpdateTime(del.getLastUpdateTime());
        model.setDisplayCount(del.getDisplayCount());
        model.setDisplayTime(del.getDisplayTime());
        model.setSource(del.getSource());
        model.setIsCourseOnly(del.getIsCourseOnly());
        model.setInputType(del.getInputType());
        model.setImportType(del.getImportType());
        model.setSourceType(del.getSourceType());
        model.setParentQuestionId(del.getParentQuestionId());
        model.setIsSubQuestion(del.getIsSubQuestion());
        model.setIsSubjective(del.getIsSubjective());
        model.setShuffle(del.getShuffle());
        model.setSerialNumber(del.getSerialNumber());
        model.setQuestionVersion(del.getQuestionVersion());
        model.setDifference(del.getDifference());
        model.setPerceiver(del.getPerceiver());
        model.setMasterLevel(del.getMasterLevel());
        model.setResponseTime(del.getResponseTime());
        model.setQuestionInnerTypeId(del.getQuestionInnerTypeId());
        model.setIsPublish(del.getIsPublish());
        model.setPublisherId(del.getPublisherId());
        model.setPublishTime(del.getPublishTime());
        model.setMediaType(del.getMediaType());
        model.setMediaDuration(del.getMediaDuration());
        model.setUseType(del.getUseType());
        model.setQualityGrade(del.getQualityGrade());
        model.setSequenceIndex(del.getSequenceIndex());

        return model;
    }
    //endregion

    //region 从试题模型得到试题删除模型

    /**
     * 从试题模型得到试题删除模型
     *
     * @param question the question
     * @return the question deleted
     */
    public static QuestionDeleted copyFromQuestion(Question question) {
        QuestionDeleted questionDeleted = new QuestionDeleted();

        questionDeleted.setQuestionId(question.getQuestionId());
        questionDeleted.setQuestionTypeId(question.getQuestionTypeId());
        questionDeleted.setQuestionCategory(question.getQuestionCategory());
        questionDeleted.setDifficulty(question.getDifficulty());
        questionDeleted.setTitle(question.getTitle());
        questionDeleted.setComment(question.getComment());
        questionDeleted.setScore(question.getScore());
        questionDeleted.setKeywords(question.getKeywords());
        questionDeleted.setQuestionAnalysis(question.getQuestionAnalysis());
        questionDeleted.setFieldCount(question.getFieldCount());
        questionDeleted.setCreater(question.getCreater());
        questionDeleted.setCreateTime(question.getCreateTime());
        questionDeleted.setModifier(question.getModifier());
        questionDeleted.setModifyTime(question.getModifyTime());
        questionDeleted.setStatus(question.getStatus());
        questionDeleted.setIsCheck(question.getIsCheck());
        questionDeleted.setChecker(question.getChecker());
        questionDeleted.setCheckTime(question.getCheckTime());
        questionDeleted.setLastUpdateTime(question.getLastUpdateTime());
        questionDeleted.setDisplayCount(question.getDisplayCount());
        questionDeleted.setDisplayTime(question.getDisplayTime());
        questionDeleted.setSource(question.getSource());
        questionDeleted.setIsCourseOnly(question.getIsCourseOnly());
        questionDeleted.setInputType(question.getInputType());
        questionDeleted.setImportType(question.getImportType());
        questionDeleted.setSourceType(question.getSourceType());
        questionDeleted.setParentQuestionId(question.getParentQuestionId());
        questionDeleted.setIsSubQuestion(question.getIsSubQuestion());
        questionDeleted.setIsSubjective(question.getIsSubjective());
        questionDeleted.setShuffle(question.getShuffle());
        questionDeleted.setSerialNumber(question.getSerialNumber());
        questionDeleted.setQuestionVersion(question.getQuestionVersion());
        questionDeleted.setDifference(question.getDifference());
        questionDeleted.setPerceiver(question.getPerceiver());
        questionDeleted.setMasterLevel(question.getMasterLevel());
        questionDeleted.setResponseTime(question.getResponseTime());
        questionDeleted.setQuestionInnerTypeId(question.getQuestionInnerTypeId());
        questionDeleted.setIsPublish(question.getIsPublish());
        questionDeleted.setPublisherId(question.getPublisherId());
        questionDeleted.setPublishTime(question.getPublishTime());
        questionDeleted.setMediaType(question.getMediaType());
        questionDeleted.setMediaDuration(question.getMediaDuration());
        questionDeleted.setUseType(question.getUseType());
        questionDeleted.setQualityGrade(question.getQualityGrade());
        questionDeleted.setSequenceIndex(question.getSequenceIndex());
        questionDeleted.setDeteleTime(DateUtil.getCurrentTimeToShowString());

        return questionDeleted;
    }
    //endregion

    //region 从试题内容模型得到试题删除内容模型

    /**
     * 从试题内容模型得到试题删除内容模型
     *
     * @param content the content
     * @return the question content deleted
     */
    public static QuestionContentDeleted copyFromQuestionContent(QuestionContent content) {
        QuestionContentDeleted questionContentDeleted = new QuestionContentDeleted();

        questionContentDeleted.setQuestionId(content.getQuestionId());
        questionContentDeleted.setContentXml(content.getContentXml());
        questionContentDeleted.setContentJson(content.getContentJson());

        return questionContentDeleted;
    }
    //endregion

    //region 从试题删除内容模型得到试题内容模型

    /**
     * 从试题删除内容模型得到试题内容模型
     *
     * @param contentDeleted the content deleted
     * @return the question content
     */
    public static QuestionContent copyFromQuestionContentDelete(QuestionContentDeleted contentDeleted) {
        QuestionContent questionContent = new QuestionContent();

        questionContent.setQuestionId(contentDeleted.getQuestionId());
        questionContent.setContentXml(contentDeleted.getContentXml());
        questionContent.setContentJson(contentDeleted.getContentJson());

        return questionContent;
    }
    //endregion

    //region 从试题筛选模型转换成试题模型

    /**
     * 从试题筛选模型转换成试题模型.
     *
     * @param row                    the row
     * @param getQuestionCategoryIds the get question category ids
     * @param questionType           the question type
     * @param questionModel          the question model
     * @return the question mode from row
     */
    public static QuestionModel getQuestionModeFromRow(QueryFilterModelDto row, List<String> getQuestionCategoryIds,
                                                       QuestionType questionType, QuestionModel questionModel) {

        // 试题标识
        if (StringUtil.isEmpty(questionModel.getI1())) {
            questionModel.setI1(row.getQuestionId());
        }
        // 试题标题
        if (StringUtil.isEmpty(questionModel.getI33())) {
            questionModel.setI33(row.getTitle());
        }
        // 试题题型标识
        questionModel.setI3(row.getQuestionInnerTypeId());
        // 所属课程标识
        questionModel.setI4(row.getItembankId());
        // 所属章节标识
        questionModel.setI5(row.getChapterId());
        // 选项列表
        // item.I6
        // 正确答案
        // item.I7
        // 试题类别
        questionModel.setI8(getQuestionCategoryIds);
        // 试题分数
        questionModel.setI9(row.getScore());
        // 试题解析
        if (StringUtil.isEmpty(questionModel.getI10())) {
            questionModel.setI10(row.getQuestionAnalysis());
        }
        // 是否乱序
        if (questionModel.getI11() == new Integer(-1)) {
            questionModel.setI11(row.getShuffle());
        }
        // 关键字
        questionModel.setI12(row.getKeywords());
        // 难度系数
        //
        questionModel.setI13(row.getDifficulty());
        // 所属机构标识
        //item.I14
        // 学生作答结果
        //item.I15
        // 学生作答得分
        //item.I16
        // 子试题个数
        //item.I17
        // 所属APP
        //item.I18
        // 媒体定义
        questionModel.setI9(row.getMediaType());
        // 媒体时长
        questionModel.setI20(row.getMediaDuration());
        // 用户自定义
        //item.I21
        // 创建者标识
        questionModel.setI22(row.getCreater());
        // 添加时间
        questionModel.setI23(row.getCreateTime());
        // 最后更新时间
        questionModel.setI24(row.getLastUpdateTime());
        // 审核状态
        questionModel.setI25(row.getIsCheck());
        // 掌握程度
        questionModel.setI26(row.getMasterLevel());
        // 认知分类
        questionModel.setI27(row.getPerceiver());
        // 试题编号
        questionModel.setI28(row.getSerialNumber());
        // 曝光时间
        questionModel.setI29(row.getDisplayTime());
        // 题型标识
        questionModel.setI30(questionType.getSectionID());
        // 建议答题时间 (分钟)
        questionModel.setI31(row.getResponseTime());
        // 试题主客观（客观题0，主观题1）
        questionModel.setI32(row.getIsSubjective());
        // 引用数量
        questionModel.setI34(row.getUseCnt());
        // 停/启用状态
        questionModel.setI35(row.getStatus());

        return questionModel;
    }
    //endregion

    //region 从map得到试题筛选模型

    /**
     * 从map得到试题筛选模型.
     *
     * @param itemBankId the item bank id
     * @param map        the map
     * @return the filter model
     */
    public static FilterModelDto getFilterModel(String itemBankId, Map<String, Object> map) {
        FilterModelDto filterModelDto = new FilterModelDto();

        filterModelDto.setItembankId(itemBankId);
        //试题代码
        if (map.containsKey("code")) {
            filterModelDto.setCode(map.get("code").toString());
        }
        //活动状态
        if (map.containsKey("activeStatus")) {
            filterModelDto.setActiveStatus(map.get("activeStatus").toString());
        }
        //当前页码
        if (map.containsKey("pageNumber")) {
            filterModelDto.setPageNumber(Integer.valueOf(map.get("pageNumber").toString()));
        }
        //每页行数
        if (map.containsKey("pageSize")) {
            filterModelDto.setPageSize(Integer.valueOf(map.get("pageSize").toString()));
        }
        //排序字段
        if (map.containsKey("sortName")) {
            filterModelDto.setSortName(map.get("sortName").toString());
        }
        //排序类型
        if (map.containsKey("sortOrder")) {
            filterModelDto.setSortOrder(map.get("sortOrder").toString());
        }
        //开始时间
        if (map.containsKey("beginTime")) {
            filterModelDto.setBeginTime(map.get("beginTime").toString());
        }
        //结束时间
        if (map.containsKey("endTime")) {
            filterModelDto.setEndTime(map.get("endTime").toString());
        }
        //试题标题
        if (map.containsKey("title")) {
            filterModelDto.setTitle(map.get("title").toString());
        }
        //应用类别标识列表
        if (map.containsKey("categoryList")) {
            filterModelDto.setCategoryList(map.get("categoryList").toString());
        }
        //章节标识列表
        if (map.containsKey("chapterList")) {
            filterModelDto.setChapterList(map.get("chapterList").toString());
        }
        //最小难度
        if (map.containsKey("minDifficult")) {
            filterModelDto.setMinDifficult(map.get("minDifficult").toString());
        }
        //最大难度
        if (map.containsKey("maxDifficult")) {
            filterModelDto.setMaxDifficult(map.get("maxDifficult").toString());
        }
        //题型标识
        if (map.containsKey("questionTypeId")) {
            filterModelDto.setQuestionTypeId(map.get("questionTypeId").toString());
        }
        //认知分类
        if (map.containsKey("perceiverId")) {
            filterModelDto.setPerceiverId(map.get("perceiverId").toString());
        }
        //掌握程度
        if (map.containsKey("masterLevel")) {
            filterModelDto.setMasterLevel(map.get("masterLevel").toString());
        }
        //审核状态
        if (map.containsKey("checkStatus")) {
            filterModelDto.setCheckStatus(map.get("checkStatus").toString());
        }
        //包含试题标识列表
        if (map.containsKey("includeIdList")) {
            filterModelDto.setIncludeIdList(map.get("includeIdList").toString());
        }
        //排除试题标识列表
        if (map.containsKey("excludeIdList")) {
            filterModelDto.setExcludeIdList(map.get("excludeIdList").toString());
        }

        return filterModelDto;
    }
    //endregion

    //region 根据试题筛选模型得到试题通用输入模型

    /**
     * 根据试题筛选模型得到试题通用输入模型
     *
     * @param model                 the model
     * @param questionCategoryNames the question category names
     * @return question import model
     */
    public static QuestionImportModel combinationContent(QueryFilterModelDto model, List<String> questionCategoryNames) {
        QuestionImportModel questionImportModel = new QuestionImportModel();
        questionImportModel.setQuestionType("[题型] " + model.getQuestionTypeName());
        questionImportModel.setChapter("[章节] " + model.getChapterName());
        if (questionCategoryNames != null) {
            questionImportModel.setQuestionCategory("[类别] " + String.join(",", questionCategoryNames));
        }
        questionImportModel.setQuestionAnalysis("[解析] " + model.getQuestionAnalysis());
        questionImportModel.setDifficulty("[难度] " + CommonFunction.getDifficulty(model.getDifficulty()));
        questionImportModel.setScore("[分数] " + model.getScore());
        questionImportModel.setErrorInfo("");
        questionImportModel.setRemark("");
        return questionImportModel;
    }
    //endregion

    //region 根据试题输出模型得到word输出map

    /**
     * 根据试题输出模型得到word输出map.
     *
     * @param questionImportModel 试题输出模型
     * @param isSub               是否是子试题
     * @return the data map
     */
    public static Map<String, Boolean> getDataMap(QuestionImportModel questionImportModel, boolean isSub) {
        //创建一个linkedList集合
        Map<String, Boolean> dataMap = new LinkedHashMap<>();

        //题型
        dataMap.put(questionImportModel.getQuestionType(), false);

        if (!isSub) {
            //章节
            dataMap.put(questionImportModel.getChapter(), false);
            //类别
            dataMap.put(questionImportModel.getQuestionCategory(), false);
        }
        //题干
        dataMap.put(questionImportModel.getPrompt(), true);
        //选项
        dataMap.put(questionImportModel.getChoices().get(0), true);
        //答案
        dataMap.put(questionImportModel.getAnswer().get(0), true);
        //解析
        dataMap.put(questionImportModel.getQuestionAnalysis(), true);
        if (!isSub) {
            //难度
            dataMap.put(questionImportModel.getDifficulty(), false);
        }
        //分数
        dataMap.put(questionImportModel.getScore(), false);

        dataMap.put("", false);

        return dataMap;
    }
    //endregion
}
